﻿// -------------------------------------------------------------------------
//    @FileName         :    NFLoginNoticeData.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-28
//    @Email            :    445267987@qq.com
//    @Module           :    NFLoginNoticeData
//
// -------------------------------------------------------------------------

#include "NFLoginNoticeData.h"

#include <NFPacketSendUtil.h>

#include "proto_cs.pb.h"

NFLoginNoticeData::NFLoginNoticeData()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFLoginNoticeData::~NFLoginNoticeData()
{
}

int NFLoginNoticeData::CreateInit()
{
    has_predefine_notice = false;
    return 0;
}

int NFLoginNoticeData::ResumeInit()
{
    return 0;
}

int NFLoginNoticeData::AfterAllConnectAndAllDescStore(NF_SERVER_TYPE serverType)
{
    if (serverType != NF_ST_LOGIC_SERVER) return 0;

    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetRunMode())
    {
        Load(NF_ST_LOGIC_SERVER);
    }
    return 0;
}

int NFLoginNoticeData::LoadFromDB(const proto_ff::tbLoginNoticeData &dbData)
{
    m_stData.FromPb(dbData);
    if (m_stData.notice.data.size() != m_stData.notice.data.max_size())
    {
        m_stData.notice.data.resize(m_stData.notice.data.max_size());
    }
    has_predefine_notice = true;
    return 0;
}

int NFLoginNoticeData::SaveToDB(proto_ff::tbLoginNoticeData &dbData)
{
    m_stData.ToPb(&dbData);
    return 0;
}

int NFLoginNoticeData::InitConfig()
{
    has_predefine_notice = false;
    m_stData.id = GetDbId();
    m_stData.notice.data.resize(m_stData.notice.data.max_size());
    MarkDirty();
    return 0;
}

int NFLoginNoticeData::GetDbId()
{
    return 1;
}

int NFLoginNoticeData::SetLoginNoticeData(uint64_t begin_time, uint64_t end_time, const std::string& title, const std::string& notice, int index, bool is_pop)
{
    if (index <= 0 || index > (int)m_stData.notice.data.size())
    {
        LOGSVR_ERROR("loginnotice index err:" << index);
        return -1;
    }

    SingleLoginData *tmpData = &m_stData.notice.data[index - 1];
    tmpData->Init();
    tmpData->index = index;
    tmpData->begin_time = begin_time;
    tmpData->end_time = end_time;
    tmpData->is_pop = is_pop;
    tmpData->title = title;
    tmpData->notice = notice;
    return 0;
}

int NFLoginNoticeData::OnPlayerLogin(NFPlayer *pPlayer)
{
    uint64_t now = NF_ADJUST_TIMENOW();
    proto_ff::Proto_CSRsp rsp;
    proto_ff::Proto_CSLoginNoticeNtf *login_notice_ntf = rsp.mutable_login_notice_ntf();
    for (int i = 0; i < (int) m_stData.notice.data.size(); ++i)
    {
        if (m_stData.notice.data[i].index > 0)
        {
            if (now >= m_stData.notice.data[i].begin_time && now <= m_stData.notice.data[i].end_time)
            {
                m_stData.notice.data[i].ToPb(login_notice_ntf->add_data());
            }
        }
    }

    if (login_notice_ntf->data_size() > 0)
    {
        NFPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_LOGIN_NOTICE_NTF, &rsp);
    }
    return 0;
}
